package com.example.demo.controller;


import com.example.demo.bean.PageConfig;
import com.example.demo.dto.BindDto;
import com.example.demo.service.NfcBindService;
import com.example.demo.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.example.demo.vo.NfcVo;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/nfc")
@RequiredArgsConstructor
public class NfcController {

    private final NfcBindService nfcService;


    @GetMapping("/dropdown")
    public Result<List<PageConfig>> dropdown(){
        return Result.success(nfcService.dropdown());
    }


    /** 绑定/更新 */
    @PostMapping("/bind")
    public Result<Void> bind(@Valid @RequestBody BindDto dto) {
        nfcService.bind(dto.getNfcId(), dto.getPageId());
        return Result.success(null);
    }

    /** 解绑 */
    @DeleteMapping("/bind")
    public Result<Void> unbind(@Valid @RequestBody BindDto dto) {
        nfcService.unbind(dto.getNfcId());
        return Result.success(null);
    }

    /** 管理列表 */
    @GetMapping("/list")
    public Result<List<NfcVo>> list() {
        return Result.success(nfcService.listAll());
    }

    /** 刷卡跳转（前端或设备调用） */
    @GetMapping("/page/{nfcId}")
    public Result<PageConfig> getPage(@PathVariable String nfcId) {
        PageConfig page = nfcService.getPageByNfc(nfcId);
        return page == null ? null : Result.success(page);
    }


}
